package com.accounting.client.frontend.product.handlers;

import com.accounting.client.remote.Services;
import com.accounting.client.remote.obj.ProductCatalog;
import com.accounting.client.remote.transfer.AddProductCatalogRequest;
import com.accounting.client.session.ErrorHandler;
import com.accounting.client.session.SessionManager;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

import corelib.client.delegate.Action;
import corelib.client.resource.CommonResources;
import corelib.client.ui.widget.Size;
import corelib.client.ui.widget.WidgetListener;
import corelib.client.ui.widget.WindowHandle;
import corelib.client.ui.widget.button.IsButton;
import corelib.client.ui.widget.dialog.SimpleDialogHandler;
import corelib.client.ui.widget.form.LabelWidget;
import corelib.client.ui.widget.form.LazyInitializer;
import corelib.client.ui.widget.form.TextAreaField;
import corelib.client.ui.widget.form.TextField;
import corelib.client.ui.widget.layout.RelativeColumn;
import corelib.client.ui.widget.messagebox.SimpleMessageBox;

public class CreateProductCatalogHandler extends SimpleDialogHandler {

	private ProductCatalog parentCatalog;
	private TextField   nameField;
	private TextField   cNameField;
	private TextAreaField    descriptionField;
	private Action action;
	
	public CreateProductCatalogHandler(ProductCatalog catalog, Action action){
		this.parentCatalog = catalog;
		this.action = action;
	}
	
	@Override
	public String getHeading() {
		return "Append New Product Catalog";
	}

	@Override
	public Size getSize() {
		return new Size(500, 500);
	}

	@Override
	public Widget getContentPanel() {
		int lw = 150;
		int fw = 270;
		int gap = 12;
		
		FlowPanel panel = new FlowPanel();
		panel.getElement().getStyle().setPadding(20, Unit.PX);
		
		RelativeColumn col = new RelativeColumn();
		col.setRowSpacing(12);
		col.addRow().add(new LabelWidget("Create New Product Catalog Under " + parentCatalog.getCName()).setBold(true),lw+fw);
		
		col.setRowSpacing(12);
		col.addRow()
		   .add(LazyInitializer.createLabel("Product Catalog Name"),lw)
		   .addSpace(gap)
		   .add(nameField = LazyInitializer.createTextBox("Product Catalog Name"),fw);
		
		col.addRow()
		   .add(LazyInitializer.createLabel("Product Catalog Chinese Name"),lw)
		   .addSpace(gap)
		   .add(cNameField = LazyInitializer.createTextBox("Product Catalog Chinese Name"),fw);
		
		col.addRow()
		   .add(new LabelWidget("Description"), lw+fw);
		
		col.addRow()
		   .add(descriptionField = LazyInitializer.createTextArea(), lw+fw);
		
		descriptionField.setHeight(200);
		descriptionField.setWidth("100%");
		panel.add(col.getPanel());
		return panel;
	}

	@Override
	public IsButton[] getButtons() {
		return new IsButton[] {
			   new IsButton("OK", CommonResources.INSTANCE.accept16(), new WidgetListener(){
				@Override
				public void valueUpdated(Widget widget) {
					if (parentCatalog == null){
						SimpleMessageBox.error("Error", "Your must select parent catalog.", null);
						return;
					}
					
					String passport = SessionManager.getPassport();
					AddProductCatalogRequest request = new AddProductCatalogRequest();
					request.setPID(parentCatalog.getId());
					request.setName(nameField.getTrimmedText());
					request.setCName(cNameField.getTrimmedText());
					request.setDescription(descriptionField.getValue());
					
					Services.ProductCatalogService.AddProductCatalog(passport, request,
					   new AsyncCallback<Boolean>(){
							@Override
							public void onFailure(Throwable caught) {
								ErrorHandler.handleServiceError(caught);
							}
							
							@Override
							public void onSuccess(Boolean result) {
								if (result == Boolean.TRUE){
									closeDialog();
									if (action != null){
										action.run();
									}
								}
							}
					   }
					);
				}
			   })
		};
	}

	@Override
	public void onClosing(WindowHandle windowHandle) {
		windowHandle.close();
	}

}
